//
//  FSWSegmentedControl.m
//  quhuolang_driver
//
//  Created by 曹欣欣 on 2018/3/13.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWSegmentedControl.h"
#import "FSWSegmentedControlLabel.h"
#define MYHEIGHT self.frame.size.height - 5//宏定义（全局定义）

@interface FSWSegmentedControl ()

@property (nonatomic, strong)NSMutableArray* buttons;
@property (nonatomic, strong)NSMutableArray* views;
@property (nonatomic, strong)NSMutableArray* labels;
//@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)FSWSegmentedControlLabel *controlLabel;
@property (assign, nonatomic)   NSInteger width;
@end

@implementation FSWSegmentedControl
{
//    NSInteger width;
}
+(instancetype)headView{
    return [[[NSBundle mainBundle]loadNibNamed:@"FSWSegmentedControl" owner:nil options:nil]lastObject];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.backgroundColor = [UIColor whiteColor];
    self.lineView.backgroundColor = XCMainColor;
}

-(void)showTitles:(NSArray *)titleArray andItemWidth:(NSInteger)itemWidth andItemHeight:(NSInteger)itemHeight backblock:(void (^)(NSInteger index))block{
     self.width = itemWidth;
    self.contentSize = CGSizeMake(itemWidth * titleArray.count, itemHeight);
    
    
    
     [self setUI:titleArray block:block];
}
 
#pragma mark --Private Method
-(void)setUI:(NSArray *)array block:(void(^)(NSInteger))block
{
    @weakify(self);
    [self.views removeAllObjects];
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, MYHEIGHT , self.width, 5)];
     self.lineView.backgroundColor = XCMainColor;
    
    [self addSubview:self.lineView];
     self.lineView.frame = CGRectMake(0, MYHEIGHT , self.width, 5);
    for (int i = 0; i < array.count; i++) {
        self.controlLabel = [FSWSegmentedControlLabel headView];
        self.controlLabel.frame =CGRectMake(i * self.width, 0,self.width, 70);
        if (0 == i) {
            self.controlLabel.button.selected = YES;
            self.controlLabel.subLabel.textColor = XCMainColor;
        }else{
            
        }
        self.controlLabel.button.tag = 200 + i;
        [self.controlLabel show:array[i] andSub:@"" andControlButton:^(UIButton *sender) {
            @strongify(self);
            for (FSWSegmentedControlLabel *view in self.views) {
                if (view.button.tag == sender.tag) {
                     view.subLabel.textColor = XCMainColor;
                    [self moveLine:view.frame.origin.x];
                }else{
                     view.subLabel.textColor = [UIColor colorWithHexString:@"777777"];
                }
            }
            
            if (self.block) {
                self.block(sender.tag - 200);
            }
        }];
        
        [self addSubview:self.controlLabel];
        [self.views addObject:self.controlLabel];
 
    }
 
    self.block = block;
    self.block(0);
}
- (void)orderNumberData:(NSArray *)data{
    for (FSWSegmentedControlLabel *views in self.views) {
        NSInteger i = views.button.tag -200;
        [views sublabel:data[i]];
    }
}
#pragma mark --Public Method
- (void)moveLine:(CGFloat)offset
{
    @weakify(self);
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            @strongify(self);
            self.lineView.frame = CGRectMake(offset, MYHEIGHT ,self.width, 5);
        }completion:^(BOOL finished) {
  
            @strongify(self);
            for (FSWSegmentedControlLabel* view in self.views) {
                view.backgroundColor = [UIColor whiteColor];
                view.button.selected = NO;
            }
            
            FSWSegmentedControlLabel* view = self.views[(int)(offset / self.width)];
            view.button.selected = YES;
            
            for (FSWSegmentedControlLabel *v in self.views) {
                if (v.button.tag == view.button.tag) {
                    v.subLabel.textColor = XCMainColor;
                }else{
                    v.subLabel.textColor = [UIColor colorWithHexString:@"777777"];
                }
            }
            
            if (self.contentSize.width > SCREEN_WIDTH) {
                if ((int)(offset /self.width) > 1 && (int)(offset / self.width) < self.buttons.count - 2) {
                    [UIView animateWithDuration:0.1 animations:^{
                        @strongify(self);
                        self.contentOffset = CGPointMake(offset - 2 * self.width, 0);
                    }];
                }else{ 
                    
                    if ((int)(offset / self.width) <= 1) {
                        [UIView animateWithDuration:0.1 animations:^{
                            @strongify(self);
                            self.contentOffset = CGPointMake(0, 0);
                        }];
                    }else{
                        [UIView animateWithDuration:0.1 animations:^{
                            @strongify(self);
                            self.contentOffset = CGPointMake(self.contentSize.width - 4 * self.width, 0);
                        }];
                    }
                }
            }else{
                
            }
            
        }];
    });
    
}

#pragma mark - Setter/Getter

- (NSMutableArray *)views
{
    if (!_views) {
        _views = [NSMutableArray array];
    }
    return _views;
}


@end

